package com.mszlu.blog.admin.controller;

import com.mszlu.blog.admin.common.Result;
import com.mszlu.blog.admin.entity.Admin;
import com.mszlu.blog.admin.entity.params.PageParam;
import com.mszlu.blog.admin.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

@Slf4j
@RestController
@RequestMapping("/admin/user/")
public class UserController {

    @Autowired
    private AdminService adminService;

    @PostMapping("/userInfo")
    public Result userInfo(Principal principal){

        Admin adminByUserName = adminService.findAdminByUserName(principal.getName());

        if (adminByUserName == null){
            return Result.loginError("未登录");
        }

        return Result.success(adminByUserName.getUsername());

    }

    @PostMapping("/userList")
    public Result userList(@RequestBody PageParam pageParam){

        return adminService.userList(pageParam);
    }

    @PostMapping("/editPassword")
    public Result editPassword(@RequestBody Admin admin){

        return adminService.editPasswordByUserName(admin);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Admin admin){

        return adminService.addAdmin(admin);
    }

    @GetMapping("/delete/{id}")
    public Result add(@PathVariable long id){
        boolean b = adminService.removeById(id);
        if (b == true){
            return Result.success("删除成功");
        }
        return Result.error("无该用户");
    }


}
